package com.surfilter.mq.rocketmq.impl;

import com.surfilter.mq.entity.Organization;
import com.surfilter.mq.rocketmq.OprateMessageService;
import com.surfilter.mq.rocketmq.cache.RockMqMesgManage;
import com.surfilter.mq.rocketmq.cache.RocketMqMessageStandard;
import com.surfilter.mq.util.ByteUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class OprateMessageServiceCache  implements OprateMessageService {
	
	private static Logger logger = LoggerFactory.getLogger(OprateMessageServiceCache.class);
	
	private RockMqMesgManage manage;
	
	public OprateMessageServiceCache(RockMqMesgManage manage){
		this.manage = manage;
	}

	@Override
	public void oprate(List<byte[]> messages) {
		// TODO Auto-generated method stub
		if(messages == null || messages.isEmpty())return;
		List<RocketMqMessageStandard> rmsds = new ArrayList<RocketMqMessageStandard>();
		for(byte[] byte_message : messages){
			try {
				Object obj = ByteUtils.deserialize(byte_message);
				if(obj instanceof RocketMqMessageStandard){
					RocketMqMessageStandard rmsd = (RocketMqMessageStandard)obj;
					logger.info("接收到任务ID:{}", rmsd.getMesgKey());
					rmsds.add(rmsd);
				}else{
					
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.error("格式化对象异常", e);
			}
		}
		manage.addMessages(rmsds);
	}
	
	public static void main(String[] args) {
		RocketMqMessageStandard org = new Organization();
		if(org instanceof Organization){
			System.out.println(true);
		}else{
			System.out.println(false);
		}
	}

}
